package com.qumitech.visualnovel.controller.rest;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.service.account.impl.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/optionConfig")

public class CommonConfigController {

    @Autowired
    AccountServiceImpl accountService;

    @RequestMapping(value = "/channelOptionConfig", method = RequestMethod.GET)
    public JSONObject channelOptionConfig ()
    {
        Map<String, Object> resultMap = accountService.getChannelsAndVersions();
        JSONArray channelList = (JSONArray) resultMap.get("channels");
        JSONArray newChannelList = new JSONArray();
        for(int i=0;i<channelList.size();i++)
        {
            JSONObject item = new JSONObject();
            item.put("id", channelList.get(i));
            item.put("text", channelList.get(i));
            newChannelList.add(item);
        }
        return new BaseResponse<>(newChannelList);
    }

    @RequestMapping(value = "/versionOptionConfig", method = RequestMethod.GET)
    public JSONObject versionOptionConfig ()
    {
        Map<String, Object> resultMap = accountService.getChannelsAndVersions();
        List<String> versionList = (List<String>) resultMap.get("versions");
        Collections.sort(versionList, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String versionStr1="";
                String versionStr2="";
                if (o1 != null) {
                    String[] versionStrList1 = o1.split("_");
                    if (versionStrList1.length==2 )
                    {
                        versionStr1 = versionStrList1[1];
                    }
                }
                if (o2 != null) {
                    String[] versionStrList2 = o2.split("_");

                    if (versionStrList2.length == 2) {
                        versionStr2 = versionStrList2[1];
                    }
                }

                return versionStr2.compareTo(versionStr1);
            }
        });

        JSONArray newChannelList = new JSONArray();
        for(int i=0;i<versionList.size();i++)
        {
            JSONObject item = new JSONObject();
            item.put("id", versionList.get(i));
            item.put("text", versionList.get(i));
            newChannelList.add(item);
        }
        return new BaseResponse<>(newChannelList);
    }


    @RequestMapping(value = "/playsOptionConfig", method = RequestMethod.GET)
    public JSONObject playsOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"app\", \"text\": \"app\"}, {\"id\":\"voiceroom\", \"text\": \"voiceroom\"}]");
        return new BaseResponse<>(items);
    }

    @RequestMapping(value = "/propsOptionConfig", method = RequestMethod.GET)
    public JSONObject propsOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"coins\", \"text\": \"coins\"},{\"id\":\"diamond\", \"text\": \"diamond\"}," +
                "{\"id\":\"hicoin\", \"text\": \"hicoin\"}]");
        return new BaseResponse<>(items);
    }

    @RequestMapping(value = "/outputOptionConfig", method = RequestMethod.GET)
    public JSONObject outputOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"1\", \"text\": \"充值购买\"}, {\"id\":\"2\", \"text\": \"任务奖励\"}, " +
                "{\"id\":\"3\", \"text\": \"签到奖励\"}, {\"id\":4, \"text\": \"注册赠送\"}, {\"id\":5, \"text\": \"一元上车\"}," +
                "{\"id\":6, \"text\": \"钻石兑换\"},{\"id\":7, \"text\": \"答题段位奖励\"},{\"id\":8, \"text\": \"猜歌高手结算\"}," +
                "{\"id\":9, \"text\": \"控制台补偿\"}]");
        return new BaseResponse<>(items);
    }

    @RequestMapping(value = "/commissionOptionConfig", method = RequestMethod.GET)
    public JSONObject commissionOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"100\", \"text\": \"语音房送礼\"}, " +
                "{\"id\":\"101\", \"text\": \"非语言房送礼\"}, {\"id\":102, \"text\": \"语音房发弹幕\"}," +
                "{\"id\":103, \"text\": \"花费钻石兑换金豆\"}, {\"id\":104, \"text\": \"猜歌词房费\"}," +
                "{\"id\":105, \"text\": \"知识达人房费\"},{\"id\":106, \"text\": \"狼人杀房费\"}," +
                "{\"id\":107, \"text\": \"你画我猜换词\"},{\"id\":108, \"text\": \"滴滴花费\"}]");
        return new BaseResponse<>(items);
    }

    @RequestMapping(value = "/walletoutputOptionConfig", method = RequestMethod.GET)
    public JSONObject walletoutputOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"10001\", \"text\": \"收益产出\"}]");
        return new BaseResponse<>(items);
    }

    @RequestMapping(value = "/walletcommissionOptionConfig", method = RequestMethod.GET)
    public JSONObject walletcommissionOptionConfig ()
    {
        JSONArray items = JSONArray.parseArray("[{\"id\":\"10000\", \"text\": \"收益消耗\"}]");
        return new BaseResponse<>(items);
    }

}
